#include "pe_ccparticlesystem.h"

#include "CCQTSceneDirector.h"
#include "constantManager.h"
#include "cocoext/ccparticlesystem_ext.h"
PE_CCParticleSystem::PE_CCParticleSystem(QtVariantPropertyManager *variantManager):PEditor(variantManager)
{
    QDir  dir(ConstantManager::sharedManager()->getResourcePath());
	QStringList filter ;
	filter << "*.plist";
	m_particleFileName = dir.entryList(filter);
	m_particleFileName.insert(0, "default");
    m_positionTypeList << "PositionFree" << "PositionGroup" << "PositionRelative"; 
 
}

QtProperty *PE_CCParticleSystem::initUI()
{
    QtVariantProperty * item = addProperty(QtVariantPropertyManager::enumTypeId() , "ParticleFile");
    item->setAttribute(QLatin1String("enumNames"), m_particleFileName);
    
    item = addProperty(QtVariantPropertyManager::enumTypeId() , "PositionType");
    item->setAttribute(QLatin1String("enumNames"), m_positionTypeList);
    return m_topItem;
}

void PE_CCParticleSystem::refresh()
{
    CCParticleSystem_Ext * psys = dynamic_cast<CCParticleSystem_Ext*> (CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
    if(psys ==NULL) return;
    Property("ParticleFile")->setValue(m_particle2Index.value((int)psys));
    int posType = psys->m_current->getPositionType();
    if (posType == kCCPositionTypeFree)
    {
        Property("PositionType")->setValue(0);
    }        
    else if (posType == kCCPositionTypeGrouped)
    {
        Property("PositionType")->setValue(1);
    }        
    else if (posType == kCCPositionTypeRelative)
    {
        Property("PositionType")->setValue(2);
    } 
    
}

void PE_CCParticleSystem::valueChanged(QtProperty *property, QVariant variant)
{
    CCParticleSystem_Ext * psys = dynamic_cast<CCParticleSystem_Ext*> (CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
    if (property == Property("ParticleFile"))
    {
        int plistIndex = variant.value<int>();
        QString fileName = m_particleFileName.at(plistIndex);
        m_particle2Index.insert((int)psys, plistIndex);
        if (fileName != "default")
		{
            psys->reloadParticle(fileName.toLocal8Bit().data());
		}else
        {
            psys->reloadParticle("myweaponfire.plist");   
        }
    }
    if (property == Property("PositionType"))
    {
        int index = variant.value<int>();
        QString typeName = m_positionTypeList.at(index);
        if (typeName == "PositionFree")
        {
            psys->m_current->setPositionType(kCCPositionTypeFree);
        }        
        else if (typeName == "PositionGroup")
        {
            psys->m_current->setPositionType(kCCPositionTypeGrouped);
        }        
        else if (typeName == "PositionRelative")
        {
            psys->m_current->setPositionType(kCCPositionTypeRelative);
        } 
    }
}
